package com.aliyun.messagemgt.common.convert;

import com.aliyun.messagemgt.common.constant.PushConstant;
import com.aliyun.messagemgt.common.dto.send.MsgPushDTO;
import com.aliyun.messagemgt.common.dto.send.MsgRecordVO;
import com.aliyun.messagemgt.common.dto.send.Receiver;
import com.aliyun.messagemgt.common.dto.send.SystemMsgPushDTO;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: MsgPushVOConverter
 * @Description:
 * @Author: wugongzi
 * @Date: 2021/9/14 18:12
 * @Version: 1.0
 */
@Mapper
public interface MsgPushVOConverter {
    MsgPushVOConverter INSTANCE = Mappers.getMapper(MsgPushVOConverter.class);

    @Mappings({
            @Mapping(constant = PushConstant.API_MSG_SOURCE_TYPE + "", target = "msgSourceType"),
            @Mapping(source = "orgMessageId", target = "uniqueId"),
    })
    MsgRecordVO msgRecordDTO2msgRecordVO(MsgPushDTO.MsgRecordDTO msgRecordDTO);

    @Mappings({
            @Mapping(source = "msgSourceCode", target = "sourceCode"),
            @Mapping(source = "msgSourceName", target = "sourceName"),
            @Mapping(source = "sceneCode", target = "sceneCode"),
            @Mapping(source = "sceneName", target = "sceneName"),
            @Mapping(expression = "java(processMessageRecordList(msgRecordVO))", target = "messageRecordList"),
    })
    SystemMsgPushDTO msgRecordVO2systemMsgPushDTO(MsgRecordVO msgRecordVO);

    default List<SystemMsgPushDTO.SystemMsgRecordDTO> processMessageRecordList(MsgRecordVO msgRecordVO) {
        List<SystemMsgPushDTO.SystemMsgRecordDTO> messageRecordList = new ArrayList<>();
        SystemMsgPushDTO.SystemMsgRecordDTO systemMsgRecordDTO = new SystemMsgPushDTO.SystemMsgRecordDTO();
        List<Receiver> receivers = msgRecordVO.getReceivers();
        // userId列表,需和员工工号、手机号列表一一对应，不需过滤
        List<String> receiveUserIds = receivers.stream().map(Receiver::getReceiverId).collect(Collectors.toList());
        // 员工工号列表,需和userId、手机号列表一一对应，不需过滤
        List<String> receiveEmployeeNos = receivers.stream().map(Receiver::getEmployeeCode).collect(Collectors.toList());
        // 手机号列表,需和userId、员工工号列表一一对应，不需过滤
        List<String> receiveUserPhones = receivers.stream().map(Receiver::getReceiverMobilePhone).collect(Collectors.toList());
        systemMsgRecordDTO.setMessageId(msgRecordVO.getUniqueId());
        systemMsgRecordDTO.setReceiveUserIds(receiveUserIds);
        systemMsgRecordDTO.setReceiveEmployeeNos(receiveEmployeeNos);
        systemMsgRecordDTO.setReceiveUserPhones(receiveUserPhones);
        String msgAttach = msgRecordVO.getAttachmentAddress();
        if (StringUtils.isNotBlank(msgAttach)) {
            systemMsgRecordDTO.setMessageAttachment(msgAttach);
        }
        // 设置消息内容
        String messageContent = msgRecordVO.getContents().get(0).getContent();
        systemMsgRecordDTO.setMessageContent(messageContent);
        messageRecordList.add(systemMsgRecordDTO);
        return messageRecordList;
    }
}
